package com.smartkey.activity;

import com.project.smartkey.R;
import com.umeng.fb.FeedbackAgent;
import com.umeng.newxp.common.ExchangeConstants;
import com.umeng.newxp.controller.ExchangeDataService;
import com.umeng.newxp.view.ExchangeViewManager;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * 设置页
 * 
 */ 
public class SettingActivity extends BaseActivity implements OnClickListener {
	private RelativeLayout rl_check_update;
	private RelativeLayout rl_feed_back;
	private RelativeLayout rl_about;
	private RelativeLayout rl_recommend;
	
	private ImageView back;

	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

	private void initView() {
		setContentView(R.layout.setting_layout);

		rl_check_update = (RelativeLayout) findViewById(R.id.rl_check_update);
		rl_feed_back = (RelativeLayout) findViewById(R.id.rl_feed_back);
		rl_about = (RelativeLayout) findViewById(R.id.rl_about);
		rl_recommend = (RelativeLayout) findViewById(R.id.rl_recommend);
		back = (ImageView) findViewById(R.id.img_back);

		rl_check_update.setOnClickListener(this);
		rl_feed_back.setOnClickListener(this);
		rl_about.setOnClickListener(this);
		rl_recommend.setOnClickListener(this);
		back.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_check_update:// 检测更新
			progressDialog = ProgressDialog.show(SettingActivity.this, "",
					"正在检测更新", true);
			UmengUpdateAgent.update(this);
			UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {

				@Override
				public void onUpdateReturned(int arg0, UpdateResponse arg1) {
					if (progressDialog != null && progressDialog.isShowing()) {
						progressDialog.dismiss();
					}
					if (!arg1.hasUpdate) {
						Toast.makeText(SettingActivity.this, "当前已经是最新版本",
								Toast.LENGTH_SHORT).show();
					}
				}
			});
			break;
		case R.id.rl_feed_back:// 用户反馈
			FeedbackAgent agent = new FeedbackAgent(this);
			agent.startFeedbackActivity();
			break;
		case R.id.rl_about:// 关于
			aboutDialog();
			break;
		case R.id.img_back://返回
			finish();
			break;

		default:
			break;
		}
	}

	private void aboutDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.dialog);
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.about_layout, null);
		builder.setView(view);
		final AlertDialog dialog = builder.create();
		ImageView ok = (ImageView) view.findViewById(R.id.img_ok);
		ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}
}
